/*


	USART functions


*/


void usart_putchar(char c, int which_one);
unsigned char usart_scan(int which_one);
void usart_disp_int(int temp, int which_one);
void usart_write(char * str, int which_one);
void usart_writeln(char * str, int which_one); //Displays message on new line
void usart_init(int which_one);
unsigned char usart_getchar(int which_one);


//Defines for USART
#define 	RXB8 		1
#define 	TXB8 		0
#define 	UPE 		2
#define 	OVR 		3
#define 	FE 			4
#define 	UDRE 		5
#define 	RXC 		7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define ERROR_CHAR (unsigned char)-1

char CRLF[] = "\n\r";


void usart_write(char * str, int which_one)
{

	char temp;
	
	
	for (int i=0; i<strlen(str); i++)
	{
		temp=str[i];
		usart_putchar(temp, which_one);

	}
	

	

}



// prints new string to LCD view
void usart_writeln(char * str, int which_one)
{
	
	usart_write(CRLF, which_one);	//newline
	usart_write(str, which_one);
	
}
void usart_disp_int(int temp, int which_one)
{
	//Displays a 4 digit integer on the LCD
	
	int thousands, hundreds, tens, ones;
	
	while (temp>9999)
		temp-=10000;
	
	thousands=(((temp/10)/10)/10)+48;
	hundreds=(((temp/10)/10)%10)+48;
	tens=((temp/10)%10)+48;
	ones=(temp%10)+48;
	
	
	
	usart_putchar(thousands, which_one);
	usart_putchar(hundreds, which_one);
	usart_putchar(tens, which_one);
	usart_putchar(ones, which_one);
	
}



void usart_init(int which_one)
{
	
	if (which_one==1)
	{
	
		// USART1 initialization
		// Communication Parameters: 8 Data, 1 Stop, No Parity
		// USART1 Receiver: On
		// USART1 Transmitter: On
		// USART1 Mode: Asynchronous
		// USART1 Baud rate: 9600
		UCSR1A=0x00;
		UCSR1B=0x18;     		//(00011000)
		UCSR1C=0x06;
		
		
		//9600 bps
		//UBRR1H=0x00;			//baud rate register high
		//UBRR1L=0x67;			//103   baud rate register low


		//57600 bps
		UBRR1H=0x00;			//baud rate register high
		UBRR1L=0x10;			//103   baud rate register low
	
	}
	else if (which_one==0)
	{
		// USART0 initialization 
	   // Communication Parameters: 7 Data, 1 Stop, Even Parity
	   // USART0 Receiver: On 
	   // USART0 Transmitter: On 
	   // USART0 Mode: Asynchronous 
	   // USART0 Baud rate: 9600 
	   
	   
	   /*
		Format of registers
		
		UCSRnB	->	RXCIEn	TXCIEn	UDRIEn	RXENn	TXENn	UCSZn2	RXB8n	TXB8n
		UCSR0C 	-> 	-		UMSELn	UPMn1	UPMn0	USBSn	UCSZn1	UCSZn0	UCPOLn
		
		
	   
	   
	   */
    UCSR0A=0x00; 
	UCSR0B=0b00011000;   //Sets Frame Format UCSZ2:0; UPM1:0 and USBS  
	UCSR0C=0b00100100;	//Sets Frame Format
	UBRR0H=0x00;         //sets 9600 baud rate based on 16 MHz processor
	UBRR0L=0x67;         //sets 9600 baud rate based on 16 MHz processor
	
	}

	
	

}

unsigned char usart_scan(int which_one)
{

	if (which_one==1)
	{
		if((UCSR1A & RX_COMPLETE) && !(UCSR1A & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))){
			return (unsigned char)UDR1;
		}
		else{
			return ERROR_CHAR;
	}
	
	}
	else if (which_one==0)
	{
		if((UCSR0A & RX_COMPLETE) && !(UCSR0A & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))){
			return (unsigned char)UDR0;
		}
		else{
			return ERROR_CHAR;
		}	
	}

	return 0;	//should never reach here if good uasrt number is passe
	
}


unsigned char usart_getchar(int which_one)
{
	if (which_one==1)
	{
		char status,data='!';
		while (1)
		{
			while (((status=UCSR1A) & RX_COMPLETE)==0);
			data=UDR1;        
			if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)   return data;               
									
		}
	
	}
	else if (which_one==0)
	{
		char status,data='!';
		while (1)
		{
		  while (((status=UCSR0A) & RX_COMPLETE)==0);
		  data=UDR0;         
		  if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)   return data;               
									
		}
	
	}
	else
	{
		//bad USART number
	}

	return 0;	//should never reach here if good uasrt number is passe
	
} 


void usart_putchar(char c, int which_one)
{
	if (which_one==1)
	{
		while ((UCSR1A & 1<<UDRE)==0);
		UDR1=c;
	}
	else if (which_one==0)
	{
	   while ((UCSR0A & 1<<UDRE)==0); 
	   UDR0=c; 
	}
} 

